import {ref,type Ref} from 'vue'
import {RequestLogin} from '../apis/index'
import type{ILoginList} from "../types/type"
import {UserLoginAccount} from '../stores/Login'
import { useRouter } from 'vue-router'
import { Toast } from 'vant'
export const UserLogin=()=>{
    

    const username:Ref<string>=ref('')
    const password:Ref<string>=ref('')
    const store=UserLoginAccount()
    const router=useRouter()
    //返回到主界面
    const onClickLeft=()=> {
        router.push('/my')
    }
    //登录接口调用
    const onSubmit=async()=>{
        const data=await RequestLogin(username.value,password.value)
        const {resultCode,resultInfo,token}=data
        if(resultCode===1){
            const list:ILoginList={
                id:resultInfo.uid,
                name:resultInfo.nick,
                url:resultInfo.headerimg
            }
            console.log(list);
            
            //存store
            store.saveAccount(list)
            console.log( store.account);
            localStorage.setItem('id',list.id)
            //保存token
            localStorage.setItem('TOKEN',token)
            Toast({
                message: '登录成功',
                position: 'top',
              });
            //跳转
            router.push('/my')
            
        }else{
            Toast({
                message: '密码或账号名错误',
                position: 'top',
              });
        }
    }
    return {onClickLeft,username,password,onSubmit}
}